package wiiusej.hardwareControllers;

import java.util.HashMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;

import wiiusej.Wiimote;
import wiiusej.apptest.generalTasks.ActivateSpeaker;

public class HardwareManagerImpl extends Thread{

	private static final int NTHREADS = 100;
	
	private static final ExecutorService executor = 
		Executors.newFixedThreadPool(NTHREADS);	
	
	//Hash map wich controls the status of the hardware components of wiimote
	private static WiimoteStatus wiimoteStatus = new WiimoteStatus(); 
	
	
	private static Wiimote wiimote;
	
	//RealTimeFeedback communication queue
	private static BlockingQueue<Integer> fromRealToSoundQ;
	
	
	
	public HardwareManagerImpl(Wiimote wiimote,
			BlockingQueue<Integer> fromRealToSoundQ){
		
		this.wiimote = wiimote;
		this.fromRealToSoundQ = fromRealToSoundQ;
		
		initComponents();
	}	
	
	private void initComponents() {
		configSpeaker();
		setSoundMax();
		initSpeakerController();		
	}
	
	
	
	@Override
	public void run() {
		
		
	}	
	
	private void initSpeakerController(){
		//wake up the speaker controller
		PermanenSoundController speakerController = (PermanenSoundController) 
			new PermanenSoundController(wiimote, fromRealToSoundQ);		
		
		executor.execute(speakerController);		
	}
	
	private void configSpeaker(){
		
		//by default apcm
		Future<AtomicBoolean> future = 
    		executor.submit(new ActivateSpeaker(wiimote, wiimoteStatus.getSpeakerStatus()));    		
    		
    	try {
    		wiimoteStatus.setSpeakerStatus(future.get());	    		
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}			
		
			
	}
	
	private void setSoundMax(){
		int i;
		
		for(i = 0; i<=7; i++){
			wiimote.turnVolumeUp();			
		}
		
	}

	
	
}
